package com.jctrip.hotel.route.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.domain.dao.*;
import com.jctrip.hotel.domain.entity.*;
import com.jctrip.hotel.route.common.ThreadLocalSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 渠道接口3.0 抽象服务类
 */
@Service
public class RouteService {

    @Autowired
    private HtHotelStdInfoDao htHotelStdInfoDao;

    @Autowired
    private HtRoomTypeStdInfoDao htRoomTypeStdInfoDao;

    @Autowired
    private HtPricePolicyDao htPricePolicyDao;

    @Autowired
    private HtSourceInfoDao htSourceInfoDao;

    @Autowired
    private HtOrderRouteRecordDao htOrderRouteRecordDao;

    @Autowired
    private HtOrderSourceRecordDao htOrderSourceRecordDao;

    @Autowired
    private HtHotelMthdomInfoDao htHotelMthdomInfoDao;

    protected HtHotelStdInfo getHotelStdInfo(String hotelId, List<String> sourceCodes) {
        LambdaQueryWrapper<HtHotelStdInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtHotelStdInfo::getHotelId, hotelId);
        queryWrapper.in(HtHotelStdInfo::getSourceCode, sourceCodes);
        return htHotelStdInfoDao.selectOne(queryWrapper);
    }

    protected HtRoomTypeStdInfo getRoomTypeStdInfo(String hotelId, String roomTypeCode) {
        LambdaQueryWrapper<HtRoomTypeStdInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtRoomTypeStdInfo::getHotelId, hotelId);
        queryWrapper.eq(HtRoomTypeStdInfo::getRoomTypeCode, roomTypeCode);
        return htRoomTypeStdInfoDao.selectOne(queryWrapper);
    }

    protected HtSourceInfo getSourceInfo(String sourceId) {
        LambdaQueryWrapper<HtSourceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtSourceInfo::getSourceId, sourceId);
        return htSourceInfoDao.selectOne(queryWrapper);
    }

    protected List<HtPricePolicy> getPricePolicies() {
        String routeId = ThreadLocalSession.get().getRouteInfo().getRouteId();
        LambdaQueryWrapper<HtPricePolicy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtPricePolicy::getRouteId, routeId);
        queryWrapper.eq(HtPricePolicy::getPricePolicyStatus, 1);
        return htPricePolicyDao.selectList(queryWrapper);
    }

    protected HtOrderRouteRecord getOrderRouteRecord(String routeId, String routeOrderId) {
        LambdaQueryWrapper<HtOrderRouteRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtOrderRouteRecord::getRouteId, routeId);
        queryWrapper.eq(HtOrderRouteRecord::getRouteOrderId, routeOrderId);
        return htOrderRouteRecordDao.selectOne(queryWrapper);
    }

    protected HtOrderRouteRecord getOrderRouteRecord(String orderId) {
        LambdaQueryWrapper<HtOrderRouteRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtOrderRouteRecord::getOrderId, orderId);
        return htOrderRouteRecordDao.selectOne(queryWrapper);
    }

    protected HtOrderSourceRecord getOrderSourceRecord(String orderId) {
        LambdaQueryWrapper<HtOrderSourceRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtOrderSourceRecord::getOrderId, orderId);
        List<HtOrderSourceRecord> htOrderSourceRecords = htOrderSourceRecordDao.selectList(queryWrapper);
        return htOrderSourceRecords.stream()
                .max(Comparator.comparing(HtOrderSourceRecord::getCreateTime))
                .orElse(null);
    }

    protected HtHotelMthdomInfo getHotelMthdomInfo(String hotelId) {
        LambdaQueryWrapper<HtHotelMthdomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HtHotelMthdomInfo::getHotelId,hotelId);
        return htHotelMthdomInfoDao.selectOne(queryWrapper);
    }

}
